package org.mozhu.mboot.admin.system.auth.web.controller;

import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.mozhu.mboot.admin.system.auth.entity.AuthSupport;
import org.mozhu.mboot.admin.system.privilege.entity.Permission;
import org.mozhu.mboot.admin.system.privilege.entity.Role;
import org.mozhu.mboot.admin.system.privilege.service.PermissionService;
import org.mozhu.mboot.admin.system.privilege.service.RoleService;
import org.mozhu.mboot.core.util.BeanUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
public class AuthHelper {

    RoleService roleService;
    PermissionService permissionService;

    public Object getRoles(List<? extends AuthSupport> authList) {
        Set<Long> roleIds = Sets.newHashSet();
        for (AuthSupport auth : authList) {
            roleIds.addAll(auth.getRoleIds());
        }
        return roleService.findAllById(roleIds).stream().collect(Collectors.toMap(Role::getId, c -> BeanUtils.transBean2Map(c, "id", "name")));
    }

    public Object getPermissions(List<? extends AuthSupport> authList) {
        Set<Long> permissionIds = Sets.newHashSet();
        for (AuthSupport auth : authList) {
            permissionIds.addAll(auth.getPermissionIds());
        }
        return permissionService.findAllById(permissionIds).stream().collect(Collectors.toMap(Permission::getId, c -> BeanUtils.transBean2Map(c, "id", "name")));
    }
}
